# leetcode 102 二叉树的层序遍历

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        result = []
        queue = []
        if not root:
            return []
        queue.append(root)
        while queue:
            size = len(queue)  # size记录当前层的大小
            tmp = []
            while size:
                node = queue.pop(0)
                size -= 1
                if node:
                    tmp.append(node.val)
                    queue.append(node.left)
                    queue.append(node.right)
            if tmp:
                result.append(tmp)
        return result


